using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using SqlWebAdmin.CSharp;

/// <summary>
/// Summary description for CurrentTasksWebPart
/// </summary>
namespace SqlWebAdmin.CSharp.Controls
{
    public class CurrentTasksWebPart : System.Web.UI.WebControls.WebParts.WebPart
    {
        public CurrentTasksWebPart()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            SiteMapNode current = SiteMap.CurrentNode;
            System.Collections.SortedList sorted = new System.Collections.SortedList();

            foreach (SiteMapNode child in current.ChildNodes)
            {
                string showAsTask = child["showAsTask"];
                if( !String.IsNullOrEmpty(showAsTask) )
                {
                    if (showAsTask.Equals("true") || showAsTask.Equals("1"))
                    {
                        string queryStringKeys = child["queryStringKeys"];
                        string url = child.Url;
                        if (!String.IsNullOrEmpty(queryStringKeys))
                        {
                            string[] keys = queryStringKeys.Split(',');
                            System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
                            foreach (string key in keys)
                            {
                                if (this.Context.Session[key] != null )
                                {
                                    nvc.Add(key, (string)this.Context.Session[key]);
                                }
                                else if( !String.IsNullOrEmpty( this.Context.Request.QueryString[key] ))
                                {
                                    nvc.Add(key, this.Context.Request.QueryString[key]);
                                }
                            }

                            if (nvc.Count > 0)
                            {
                                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                                string seperator = url.Contains("?") ? "&" : "?";
                                foreach (string key in nvc.AllKeys )
                                {
                                    sb.AppendFormat("{0}{1}={2}", seperator, key, nvc[key]);
                                    seperator = "&";
                                }
                                url += sb.ToString();
                            }
                        }
                        sorted.Add(child.Title, url );
                    }
                }
            }

            foreach (System.Collections.DictionaryEntry entry in sorted )
            {
                writer.WriteLine("<div><a href=\"{0}\">{1}</a></div>", entry.Value, entry.Key );
            }
        }
    }
}